(function(){
	
	if (!window.CJ) {
		window.CJ = {};
	}

	function Banner () {
		this.name = 'Banner';
		this.rootID = '#global .aside';
		this.rootElem = $(this.rootID);
		this.bindEvent();
	}
	Banner.getData = function () {
		CJ.HttpJson.get({
			type: 'BANNERLIST',
			success: function (json) {
				Banner.successCB(json);
			}
		});
	};
	Banner.successCB = function (json) {
		this.lists  = json.bannerlist;
		this.render();
	};
	Banner.render = function (json) {
		var data, html;
		var i, len = this.lists.length;
		var template = '<li data-bannertype="{{:bannertype}}" data-contentsid="{{:contentsid}}"><b></b><img src="{{:bannerimage}}" alt="{{:bannertitle}}"></li>';
		for(i=0;i<len;i++) {
			data = this.lists[i];
			html = template.replace('{{:bannertype}}', data.bannertype)
				.replace('{{:contentsid}}', data.contentsid)
				.replace('{{:bannerimage}}', data.bannerimage)
				.replace('{{:bannertitle}}', data.bannertitle);
			$('#global .aside>ul').append(html);
		}
	};
	Banner.prototype = new EventCtl();
	Banner.prototype.bindEvent = function () {
		var self = this;
		this.rootElem.on('click', 'li', function() {
			self.enter();
		});
		this.rootElem.on('mouseenter', 'li', function() {
			if(!CJ.isRightDetailEntry) {
				$('#global li').removeClass('focus');
				$('li', self.rootElem).removeClass('select focus');
				$(this).addClass('select focus');
				CJ.Scene.setDelegate(CJ.Scene["Banner"]);
			}
		});
	};
	Banner.prototype.up = function () {
		var now = $('#global .aside li.focus');
		var prev = now.prev();
		if (prev.length > 0 ) {
			now.removeClass('select focus');
			prev.addClass('select focus');
		} else {
			CJ.Scene.promotion();
		}
	};
	Banner.prototype.down = function () {
		var now = $('li.focus', this.rootElem);
		var next = now.next();
		if (next.length > 0 ) {
			now.removeClass('select focus');
			next.addClass('select focus');
		}
	};
	Banner.prototype.left = function () {
		$('li', this.rootElem).removeClass('focus');
		CJ.Scene.channel();
	};
	Banner.prototype.back = function () {
		$('li', this.rootElem).removeClass('focus');
		$("#pop").hide();
		$("#mask").hide();
		CJ.Scene.channel();
	};
	Banner.prototype.enter = function () {
		var now = $('li.focus', this.rootElem).removeClass('focus');
		var typeIndex = now.data('bannertype');
		var type = ["promotion", "event", "poll"];
		var id = now.data('contentsid');
		// bannertype
		// 프로모션	0
		// 이벤트	1
		// 투표	2
		CJ.Scene.popupView(type, id,
					function(){ //이벤트 조회 성공 콜백
						$("#mask").show();
					},
					function(){//이벤트 창이 닫힐 때 콜백
						now.addClass('focus');
						$("#mask").hide();
						CJ.Scene.setDelegate(CJ.Scene["Banner"]);
					});
	};
	

	$.extend(CJ.Scene, {
							banner: function(){
								CJ.isRightDetailEntry = false;
								$("#wrap").removeClass().addClass('ch');//vod 화면 전환
								$('#global li').removeClass('focus');
								if ( $('#global .aside li.select').addClass('focus').length <= 0) {
									$('#global .aside li:first-child').addClass('select focus');
								}
								$("#pop").hide();
								$("#mask").hide();
								this.setDelegate(this["Banner"]);
							},
							bannerDetail: function(type, id){
								CJ.isRightDetailEntry = true;
								
							}
						});

	$(document).ready(function(){
		CJ.Scene["Banner"] = new Banner();
		Banner.getData();
	});

})();